/*
 * rot13.c: ROT13 cipher test application
 *
 */

#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

/*
  *	Total count of 26 in alphbet
  *	Give enough space for Block to read
  */
#define LENGTH 52
/*
  * Cipher Key = 13
  * Each character in alphabet maps in to the character in 13 position later
  */
#define	KEY	13

int main(void) {
	/* Needs more code. */
	/* Declaration for the block to be readed*/
	char my_input[52];
	int i=0;
	int ascii;
	size_t block = sizeof(my_input);
	ssize_t block_readed;
	
	while(1){
		/*	File Descriptor = STDIN_FILENO
		  * 	Pass user input to our String my_input
		  *	block = size of the String 
		  */
	    block_readed = read(STDIN_FILENO, my_input, block);
	    if(block_readed == 0){
	      exit(0); 	/*exit with status 0 upon any syscall*/
		}else if(block_readed == 1){
		   exit(1); /*exit with status 0 upon any syscall*/
		}
		for(i=0;i <block_readed; i++){
	  	ascii = (int)my_input[i];
			/*	Map A-M to N-Z, N-Z to A-M.
			  *	Map a-m to n-z, n-z to a-m
			  */
			if(ascii>=65 && ascii<=77)
				ascii += KEY;
			else if(ascii >=78 && ascii <=90)
				ascii -= KEY;
			else if(ascii >=97 && ascii <=109)
				ascii += KEY;
			else if(ascii >= 110 && ascii <= 122)
				ascii -= KEY;
			my_input[i] = (char)ascii;
		}
	   /*Writing to  stdout stream*/
	   write(STDOUT_FILENO, my_input, block_readed);
	}

   return 0;
}
